<template>

  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0;">
      <el-form :inline="true" :model="filters" ref="filters">
        <el-form-item label="姓名">
          <el-input v-model="filters.name" placeholder="请输入姓名" size="small"></el-input>
        </el-form-item>

        <el-form-item label="章节名称" prop="type">
          <el-select v-model="filters.type" placeholder="请选择"
                     style="width: 680px"
          >
            <el-option
              v-for="item in types"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>


        <el-form-item>
          <el-button type="primary" v-on:click="getTableData" icon="el-icon-search" size="small">搜素</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="resetForm('filters')" icon="el-icon-refresh" size="small">重置</el-button>
        </el-form-item>
      </el-form>
      <div>
        <el-button type="primary" size="small" icon="el-icon-plus" @click="add">新增</el-button>
        <el-button type="success" size="small" icon="el-icon-edit">修改</el-button>
        <el-button type="danger" size="small" icon="el-icon-delete">删除</el-button>
        <el-button type="warning" size="small" icon="el-icon-download">导出</el-button>
      </div>
      <!--列表-->
      <el-table :data="tableData"
                style="width: 100%;"
      >
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="60">
        </el-table-column>
        <el-table-column prop="name" label="姓名" sortable>
        </el-table-column>
        <el-table-column prop="tel" label="电话">
        </el-table-column>
        <el-table-column prop="sex" label="性别">
        </el-table-column>
        <el-table-column prop="type" label="类型">
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template scope="scope">
            <el-button size="small" @click="edit( scope.row)" icon="el-icon-edit" type="primary">编辑</el-button>
            <el-button type="danger" size="small" @click="del( scope.row)" icon="el-icon-close">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-col>

    <el-col :span="24" class="toolbar">
      <el-pagination layout="prev, pager, next" @current-change="selectPage" :page-size="pageSize" :total="total"
                     style="float:right;"
      >
      </el-pagination>
    </el-col>
    <!--新增界面-->
    <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="addForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="tel">
          <el-input v-model="addForm.tel" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-input v-model="addForm.sex" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="courseTypeId" style="width: 700px">
          <el-radio-group v-model="addForm.type">
            <el-radio v-for="grade in types" :label="grade.id">{{ grade.name }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false" icon="el-icon-close">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading" icon="el-icon-check">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import { getdalete, getList, getsave, getType } from '@/api/teacher/teacher'

export default {
  name: 'teacher',
  data() {
    return {
      addFormVisible: false,
      addLoading: false,
      total: 0,
      pageSize: 2,
      page: 1,
      filters: {
        type: '',
        name: ''
      },
      tableData: [],
      types: [],
      addForm: {
        id: '',
        name: '',
        tel: '',
        sex: '',
        type: ''
      },
      addFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        tel: [
          { required: true, message: '请输入电话', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请输入性别', trigger: 'blur' }
        ]
      }

    }
  },
  methods: {
    del: function(row) {
      getdalete(row.id).then(response => {
        if (response) {
          this.$message({ message: '删除成功', type: 'success' })
          this.list()
        } else {
          this.$message({ message: '删除失败', type: 'success' })
        }
      }).cache(error => {
        this.$message({ message: '数据加载失败[' + error.message + ']', type: 'error' })
      })

    },
    selectPage(val) {
      this.page = val
      this.getTableData()
    },
    getTableData() {
      console.log('123')
    },
    add() {
      this.addFormVisible = true
      for (let p in this.addForm) {
        this.addForm[p] = ''
      }
    },
    addSubmit: function() {
      getsave(this.addForm).then(response => {
        let data = response.data
        if (data) {
          this.$message({ message: '提交成功', type: 'success' })
          this.addFormVisible = false
          this.list()
        } else {
          this.$message({ message: '数据加载失败[' + message + ']', type: 'error' })
        }
      }).catch(error => {
        this.listLoading = false
        this.$message({ message: '数据加载失败[' + error.message + ']', type: 'error' })
      })

    },
    resetForm(filters) {
    },
    getFileName(files) {
      return files.slice(0, 3)
    },

    fileData(row) {
      let arr = []
      row.types.forEach((item, index) => {
        if (index > 2) {
          return
        }
        arr.push(item.name)
      })
      return arr.join(',')
    },
    list() {
      getList().then(response => {
        this.tableData = response.data
      }).catch(reason => {
        console.log(reason)
      })
    },
    type() {
      getType().then(response => {
        console.log(response.data)
        this.types = response.data
      }).catch(reason => {
        console.log(reason)
      })
    },
    edit(row) {
      for (let p in this.addForm) {
        this.addForm[p] = row[p]
      }
      this.addFormVisible = true
    }
  },

  created() {
    this.list()
    this.type()
  }
}
</script>

<style scoped>

</style>
